#pragma once
#include <iostream>
#include <unistd.h>
#include <cstring>

//服务器提供的服务，也是accpet函数只会，服务器要处理的逻辑
//这里的服务器为每个线程提供的服务是短服务，也就是说
//来一个客户端处理完后，服务端就结束服务了
class Task
{
private:
    int _sock;

public:
    Task(int sock = -1) : _sock(sock) {}

    void run()
    {

        char recvBuf[1024];
        // while (1)
        // {
        memset(recvBuf, 0, sizeof(recvBuf));
        std::cout << "waitting client send to me message......" << std::endl;
        ssize_t sz = read(_sock, recvBuf, sizeof(recvBuf) - 1);
        if (sz > 0)
        {
            recvBuf[sz] = 0;
            std::cout << "client to :" << recvBuf << std::endl;
            std::string echo_str = recvBuf;
            echo_str += "  <-----我是服务器发送的";
            write(_sock, echo_str.c_str(), echo_str.size());
        }
        else if (sz == 0)
        {
            std::cout << "client close!" << std::endl;

            // break;
        }
        else
        {
            std::cerr << "read error" << std::endl;
        }
        close(_sock);
        // break;
        // }
    }
};
#if 0
class Task
{
private:
    int _x;
    int _y;
    char _op; //操作符 +-*/%
public:
    Task() {}
    Task(int x, int y, int op) : _x(x), _y(y), _op(op)
    {
    }
    int run()
    {
        int res = 0;
        switch (_op)
        {
        case '+':
            res = _x + _y;
            break;
        case '-':
            res = _x - _y;
            break;
        case '*':
            res = _x * _y;
            break;
        case '/':
            res = _x / _y;
            break;
        case '%':
            res = _x % _y;
            break;
        default:
            std::cout << "bug!!!" << std::endl;
            break;
        }
        std::cout<<"当前消费者线程:"<<pthread_self()<< "正在处理任务："\
                <<_x<<_op<<_y << "="<<res<<std::endl;
                return res;
    }
    ~Task() {}
};
#endif